/**
 * 处理时间
 */

import { ref } from 'vue'
import { PickerTimeResult, PickerTimeOptions } from '../../../../types/tn-picker'

type TimeColumnData = {
  value: number[],
  unit: string
}

export default function useTime() {

  // 时间配置参数
  let timeOption: PickerTimeOptions
  // 时间值
  let timeValue: PickerTimeResult = {}
  // 时间列表值
  let timeColumnData = ref<TimeColumnData[]>([])
  // 选中的时间的index
  let timeSelectValue = ref<number[]>([])
  
  // 更新时间数值
  const updateTime = (date: Date) => {
    timeValue.year = date.getFullYear()
    timeValue.month = date.getMonth() + 1
    timeValue.day = date.getDate()
    timeValue.hour = date.getHours()
    timeValue.minute = date.getMinutes()
    timeValue.second = date.getSeconds()
    generateColumnData()
  }

  // 更新时间配置
  const updateTimeOption = (option: PickerTimeOptions) => {
    timeOption = option
  }

  // 更新列选中数据
  const updateTimeSelectValue = (val: number[]) => {
    // 更新选中后的数据
    let index = 0
    let updateDayIndex = 0
    if (timeOption.year) {
      timeValue.year = timeColumnData.value[index].value[val[index++]]
      updateDayIndex++
    }
    if (timeOption.month) {
      timeValue.month = timeColumnData.value[index].value[val[index++]]
      updateDayIndex++
    }
    if (timeOption.day) timeValue.day = timeColumnData.value[index].value[val[index++]]
    if (timeOption.hour) timeValue.hour = timeColumnData.value[index].value[val[index++]]
    if (timeOption.minute) timeValue.minute = timeColumnData.value[index].value[val[index++]]
    if (timeOption.second) timeValue.second = timeColumnData.value[index].value[val[index++]]
    // 如果可以设置年、月则更新对应的日期(防止闰年)
    if (timeOption.year || timeOption.month) {
      timeColumnData.value[updateDayIndex].value = getDays()
    }
  }

  // 返回结果
  const getTimeResult = (): PickerTimeResult => {
    let result: PickerTimeResult = {}
    if (timeOption.year) result.year = timeValue.year
    if (timeOption.month) result.month = timeValue.month
    if (timeOption.day) result.day = timeValue.day
    if (timeOption.hour) result.hour = timeValue.hour
    if (timeOption.minute) result.minute = timeValue.minute
    if (timeOption.second) result.second = timeValue.second
    if (timeOption.timestamp) result.timestamp = getTimestamp()
    return result
  }

  // 根据配置生成列数据
  const generateColumnData = () => {
    timeSelectValue.value = []
    timeColumnData.value = []
    let value: number[] = []
    if (timeOption.year) {
      value = getYears()
      timeSelectValue.value.push(getIndex(value, timeValue.year || 0))
      timeColumnData.value.push({ value, unit: '年'})
    }
    if (timeOption.month) {
      value = getMonths()
      timeSelectValue.value.push(getIndex(value, timeValue.month || 0))
      timeColumnData.value.push({ value, unit: '月'})
    }
    if (timeOption.day) {
      value = getDays()
      if (timeValue.day && (timeValue.day > value.length)) timeValue.day = value[value.length - 1]
      timeSelectValue.value.push(getIndex(value, timeValue.day || 0))
      timeColumnData.value.push({ value, unit: '日'})
    }
    if (timeOption.hour) {
      value = getHours()
      timeSelectValue.value.push(getIndex(value, timeValue.hour || 0))
      timeColumnData.value.push({ value, unit: '时'})
    }
    if (timeOption.minute) {
      value = getMinutes()
      timeSelectValue.value.push(getIndex(value, timeValue.minute || 0))
      timeColumnData.value.push({ value, unit: '分'})
    }
    if (timeOption.second) {
      value = getSeconds()
      timeSelectValue.value.push(getIndex(value, timeValue.second || 0))
      timeColumnData.value.push({ value, unit: '秒'})
    }
  }

  // 设置年月日时分秒列数据
  const getYears = (): number[] => {
    return generateArray(timeOption.startYear || 0, timeOption.endYear || 0)
    // timeSelectValue.value.splice(timeSelectValue.value.length - 1, 1, getIndex(years.value, timeValue.year || 0))
  }
  const getMonths = (): number[] => {
    return generateArray(1, 12)
    // timeSelectValue.value.splice(timeSelectValue.value.length - 1, 1, getIndex(months.value, timeValue.month || 0))
  }
  const getDays = (): number[] => {
    let totalDays = new Date(timeValue.year || 0, timeValue.month || 0, 0).getDate()
    return generateArray(1, totalDays)
  }
  const getHours = (): number[] => {
    return generateArray(0, 23)
    // timeSelectValue.value.splice(timeSelectValue.value.length - 1, 1, getIndex(hours.value, timeValue.hour || 0))
  }
  const getMinutes = (): number[] => {
    return generateArray(0, 59)
    // timeSelectValue.value.splice(timeSelectValue.value.length - 1, 1, getIndex(minutes.value, timeValue.minute || 0))
  }
  const getSeconds = (): number[] => {
    return generateArray(0, 59)
    // timeSelectValue.value.splice(timeSelectValue.value.length - 1, 1, getIndex(seconds.value, timeValue.second || 0))
  }

  // 获取值在数组中的位置
  const getIndex = (arr: number[], value: number): number => {
    let index = arr.indexOf(value)
    // 如果index为-1着找不到元素
    // ~(-1)=(-1)-1=0
    return ~index ? index : 0
  }

  // 生成范围内的数据
  const generateArray = (start: number, end: number): number[] => {
    end = end > start ? end : start
    // 生成数组并获取其中索引然后提取出来(获取开始和结束之间的数据)
    return [...Array(end+1).keys()].slice(start)
  }

  // 获取timestamp的值
  const getTimestamp = (): number => {
    // yyyy-mm-dd为安卓写法，不支持iOS，需要使用"/"分隔，才能二者兼容
    let date = `${timeValue.year}/${timeValue.month}/${timeValue.day} ${timeValue.hour}:${timeValue.minute}:${timeValue.second}`
    return new Date(date).getTime() / 1000
  }

  return {
    timeColumnData,
    timeSelectValue,
    updateTime,
    updateTimeOption,
    updateTimeSelectValue,
    getTimeResult
  }
}